#! /usr/bin/env python
# -*- coding: utf-8 -*-


from keras import models
import cv2
import os
import numpy as np
import my_ascll_dict



def start(data_input_queue,data_output_queue):
    model = models.load_model('./ascllmodel')


    while True:
        name_list = data_input_queue.get()

        print(name_list)

        imgset = normalization(name_list)

        npimg = np.array(imgset)

        # print(npimg.shape)
        # print(npimg[0])

        # train_images = img.resize((60000,28,28,1))
        # train_images = train_images.astype('float32')/255

        resarr = model.predict(npimg)
        res = []

        dicta = my_ascll_dict.getmyasclldict()
        #
        for x in resarr:
            # print( )
            arr = x.tolist()
            res.append(dicta[ arr.index(max(arr)) ])


        print(res)

        data_output_queue.put(res)

    #for index, x in enumerate(res):
        #print("file:%s  -->  %s" % (name_list[index], dicta[x]))



#获取tmp的文件名到数组中
def get_file_list(path):
    list_name=[]
    files = os.listdir(path)
    files.sort()
    for file in files:
        file_path = os.path.join(path, file)
        list_name.append(file_path)
    return list_name

def normalization(namelist):

    image_set=[]
    # 对每张图进行尺寸标准化和归一化
    for imgurl in namelist:
        img = cv2.imread(imgurl, 0)
        img = cv2.resize(img, (28, 28))
        img = img.reshape((28,28,1))
        img = img.astype('float32') / 255
        image_set.append(img)
    return image_set
#
# model = models.load_model('./ascllmodel')
#
#
# name_list = get_file_list(r'C:\Users\he\Desktop\tmp')
#
#
# imgset = normalization(name_list)
#
#
# npimg = np.array(imgset)
#
# #print(npimg.shape)
# #print(npimg[0])
#
# #train_images = img.resize((60000,28,28,1))
# #train_images = train_images.astype('float32')/255
#
# resarr  = model.predict(npimg)
# res = []
# #
# for x in resarr:
#     #print( )
#     arr = x.tolist()
#     res.append(arr.index(max(arr)))
#
#
# print (res)
#
# dicta = my_ascll_dict.getmyasclldict()
#
# for index,x in enumerate(res):
#     print("file:%s  -->  %s"%(name_list[index],dicta[x]))

# cv2.imshow('5',train_images[5])
# cv2.imshow('6',train_images[6])
# cv2.imshow('7',train_images[7])
#
# cv2.waitKey(0)
# cv2.destroyAllWindows()